package com.xlh.controller.conclude;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.xlh.common.Result;
import com.xlh.common.ResultEnum;
import com.xlh.constant.ConcludeContant;
import com.xlh.service.conclude.ConcludeScoreService;
import com.xlh.vo.conclude.ConcludeScoreCheckVO;
import com.xlh.vo.conclude.ExcelImportTipVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author cfz
 */

@Api(tags="ConcludeScoreController", description = "工程认证 - 成绩", value = "课程目标路由")
@RestController
@RequestMapping("conclude/score")
public class ConcludeScoreController {

    @Autowired
    private ConcludeScoreService concludeScoreService;

    @ApiOperation("成绩模板下载整体")
    @GetMapping("/downTemplateEntirety")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result downTemplateEntirety(@RequestParam("concludeId") Long concludeId,
                                       HttpServletRequest request, HttpServletResponse response) {

        concludeScoreService.downLoadTemplate(concludeId, request, response);
        return null;
    }

    @ApiOperation("成绩模板下载局部")
    @GetMapping("/downTemplateSub")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result downTemplateSub(@RequestParam("concludeId") Long concludeId,
                                  @RequestParam("checkId") Long checkId,
                                  HttpServletRequest request,
                                  HttpServletResponse response) {
        concludeScoreService.downTemplateSub(concludeId, checkId, request, response);

        return null;
    }


    @ApiOperation("考核分项导入成绩")
    @PostMapping("/updateScoreSub")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result updateScoreSub(@RequestParam("concludeId") Long concludeId, @RequestParam("checkId") Long checkId,
                                 @RequestParam(value = "file") MultipartFile file) {
        int headerIndex = 1;
        Map<String, Object> resultMap = concludeScoreService.updateScoreSub(checkId, file, headerIndex);
        if(ObjectUtil.isNotNull(resultMap.get("ERROR"))){
            return new Result(ResultEnum.UPLOAD_FILE_FAILD, resultMap.get("ERROR"));
        }
        return Result.success(resultMap.get("SUCCESS"));

    }

    @ApiOperation("整体成绩导入")
    @PostMapping("/updateScoreEntirety")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result updateScoreEntirety(@RequestParam("concludeId") Long concludeId, @RequestParam(value = "file") MultipartFile file){

        Map<String, Object> resultMap = concludeScoreService.updateScoreEntirety(concludeId, file);
        if(ObjectUtil.isNotNull(resultMap.get(ConcludeContant.ERROR_FLAGE))){
            return new Result(ResultEnum.UPLOAD_FILE_FAILD, resultMap.get(ConcludeContant.ERROR_FLAGE));
        }
        return Result.success(resultMap.get(ConcludeContant.SUCCESS_FLAGE));
    }


    @ApiOperation("分数统计")
    @GetMapping("/statistics")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result statistics(@RequestParam("concludeId") Long concludeId){

        List<ConcludeScoreCheckVO> result = concludeScoreService.statistics(concludeId);
        return Result.success(result);
    }


    @ApiOperation("整体下载")
    @GetMapping("/entirety/download")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result entiretyDownload(@RequestParam("concludeId") Long concludeId,
                                   HttpServletRequest request, HttpServletResponse response){

        concludeScoreService.entiretyDownload(concludeId, request, response);
        return null;
    }



}
